﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CRMAdminDashboard.CrmSvc;

namespace CRMAdminDashboard
{
    public partial class FormCreateRole : Form
    {

        #region Constants

        private const string RES_CREATEROLE_DUPLICATE = "RES_CREATEROLE_DUPLICATE";
        private const string RES_CREATEROLE_DUPLICATE_TITLE = "RES_CREATEROLE_DUPLICATE_TITLE";

        #endregion

        #region Fields

        /// <summary>
        /// Relationship between roles
        /// </summary>
        private RoleUserHierarchy _rUHierarchy;

        /// <summary>
        /// The CRM Service handle
        /// </summary>
        private CachingCrmService _svcHandle;

        #endregion

        #region Properties

        /// <summary>
        /// Name of the role to create
        /// </summary>
        public string NewRoleName
        {
            get
            {
                return txtRoleName.Text;
            }
        }

        /// <summary>
        /// Business Unit of the role to create
        /// </summary>
        public BusinessUnit NewRoleBU
        {
            get
            {
                return (BusinessUnit)cbBu.SelectedItem;
            }
        }

        #endregion

        #region Entry Points

        /// <summary>
        /// Standard constructor
        /// </summary>
        public FormCreateRole(RoleUserHierarchy rUHierarchy, CachingCrmService svcHandle)
        {
            this._rUHierarchy = rUHierarchy;
            this._svcHandle = svcHandle;

            InitializeComponent();
        }

        #endregion

        #region Events

        private void btnOK_Click(object sender, EventArgs e)
        {
            txtRoleName.Text = txtRoleName.Text.Trim();
            // check whether the name is a duplicate
            foreach (SystemRole role in _rUHierarchy.AllRoles.Values)
            {
                if (string.Compare(role.Name, txtRoleName.Text, true) == 0)
                {
                    MessageBox.Show(Program.GetResString(RES_CREATEROLE_DUPLICATE),
                        Program.GetResString(RES_CREATEROLE_DUPLICATE_TITLE), MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void Changed(object sender, EventArgs e)
        {
            EnableControls();
        }

        private void FormCreateRole_Load(object sender, EventArgs e)
        {
            // load all BUs in the list box
            foreach (BusinessUnit bus in BusinessUnit.GetAllBUs(this._svcHandle))
            {
                cbBu.Items.Add(bus);
            }

            EnableControls();
        }

        #endregion

        #region Local Methods

        private void EnableControls()
        {
            if (txtRoleName.Text.Trim() == string.Empty || cbBu.SelectedIndex == -1)
            {
                btnOK.Enabled = false;
            }
            else
            {
                btnOK.Enabled = true;
            }
        }

        #endregion

    }
}
